/*
 * @lc app=leetcode.cn id=589 lang=javascript
 *
 * [589] N 叉树的前序遍历
 */

// @lc code=start
/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[]}
 */
// 顺序 中左右
var preorder = function (root) {
  const res = []
  const stack=[]
  if (root === null) {
    return res
  }
  stack.push(root)
  while (stack.length) {
    const node = stack.pop()
    res.push(node.val)
    for (let i = node.children.length; i >=0; i--){
      node.children[i]&&stack.push(node.children[i])
    }
  }
  return res
    
};
// @lc code=end

